

class AccessController extends BaseController{
    def beforeInterceptor = [action:this.auth, except:['login', 'dologin']]
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ accessInstanceList: Access.list( params ), accessInstanceTotal: Access.count() ]
    }

    def show = {
        def accessInstance = Access.get( params.id )

        if(!accessInstance) {
            flash.message = "Access not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ accessInstance : accessInstance ] }
    }

    def delete = {
        def accessInstance = Access.get( params.id )
        if(accessInstance) {
            try {
                accessInstance.delete(flush:true)
                flash.message = "Access ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Access ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Access not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def accessInstance = Access.get( params.id )

        if(!accessInstance) {
            flash.message = "Access not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ accessInstance : accessInstance ]
        }
    }

    def update = {
        def accessInstance = Access.get( params.id )
        if(accessInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(accessInstance.version > version) {
                    
                    accessInstance.errors.rejectValue("version", "access.optimistic.locking.failure", "Another user has updated this Access while you were editing.")
                    render(view:'edit',model:[accessInstance:accessInstance])
                    return
                }
            }
            accessInstance.properties = params
            if(!accessInstance.hasErrors() && accessInstance.save()) {
                flash.message = "Access ${params.id} updated"
                redirect(action:show,id:accessInstance.id)
            }
            else {
                render(view:'edit',model:[accessInstance:accessInstance])
            }
        }
        else {
            flash.message = "Access not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def accessInstance = new Access()
        accessInstance.properties = params
        return ['accessInstance':accessInstance]
    }

    def save = {
        def accessInstance = new Access(params)
        if(!accessInstance.hasErrors() && accessInstance.save()) {
            flash.message = "Access ${accessInstance.id} created"
            redirect(action:show,id:accessInstance.id)
        }
        else {
            render(view:'create',model:[accessInstance:accessInstance])
        }
    }
    def login = {
    }
    def dologin = {
        def access = Access.findByUserNameAndPassword(params.userName, params.password)
        def devAccess = DevAccess.findByUserNameAndPassword(params.userName, params.password)
        if(access || devAccess){
            if(access){
                session.access = access
                session.container = access.container.id
            }
            else{
                 session.access = devAccess
            }

            DBConnection.connect()
            DBConnection.startTransaction()
            redirect(controller:'project', action:'list')
        }
        /*else if(params.userName.equals("ADMIN")){
            session.access = "admin"
            redirect(controller:'project', action:'list')
        }*/
        else{
            flash.message = "Please enter a valid username and password!"
            redirect(action:login)
        }
    }
    def logout = {
        session.access = null
        session.container = null
        //TODO check if modifications saved in sandbox
        flash.message = "Successful logout!"
        redirect(action:login)
    }
}
